/*
 *  Copyright 2019-2020 Zheng Jie
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package me.zhengjie.exception.handler;

import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;

import java.time.LocalDateTime;

/**
 * @author Zheng Jie
 * @date 2018-11-23
 */
@Data
public class ApiResponse<T> {
    private Integer status = 0;

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime timestamp;

    private String message = "操作成功";

    private T data;

    private ApiResponse() {
        timestamp = LocalDateTime.now();
    }

    private ApiResponse(Integer status, String message) {
        super();
        this.status = status;
        this.message = message;
    }

    private ApiResponse(Integer status, T data) {
        super();
        this.status = status;
        this.data = data;
    }

    private ApiResponse(String message, T data) {
        super();
        this.message = message;
        this.data = data;
    }

    private ApiResponse(Integer status, String message, T data) {
        super();
        this.status = status;
        this.message = message;
        this.data = data;
    }

    public static <T> ApiResponse<T> data(T data) {
        return new ApiResponse("操作成功", data);
    }

    public static <T> ApiResponse<T> data(T data, String message) {
        return new ApiResponse(message, data);
    }

    public static <T> ApiResponse<T> data(int status, T data, String message) {
        return new ApiResponse(status, message, data);
    }

    public static <T> ApiResponse<T> success() {
        return new ApiResponse(0, "操作成功");
    }

    public static <T> ApiResponse<T> success(String message) {
        return new ApiResponse(0, message);
    }

    public static ApiResponse error() {
        return new ApiResponse(400, "操作失败");
    }

    public static ApiResponse error(String message) {
        return new ApiResponse(400, message);
    }

    public static ApiResponse error(Integer status, String message) {
        return new ApiResponse(status, message);
    }
}


